package com.example.content.viewmodule

import com.example.content.DO_ACTION
import com.example.content.manager.GlobalMgr
import com.example.content.repository.RepositoryMgr
import com.example.content.repository.bean.FollowReq
import com.example.content.repository.bean.ProductInfo
import com.example.content.repository.bean.UserActionReq
import com.example.content.viewmodule.viewdata.CommonViewState
import com.example.core.livedata.SingleLiveEvent

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/2
 */
class DetailViewModel: BaseViewModel<CommonViewState>() {
    val productInfoResult = SingleLiveEvent<List<ProductInfo>>()
    val deleteResult = SingleLiveEvent<Boolean>()

    /**
     * 获取作品信息By id
     */
    fun getProductInfoById(idList: List<Int>) {
        launch(
            block = {
                productInfoResult.postValue(RepositoryMgr.getProductInfoById(idList).apiData())
            }
        )
    }

    /**
     * 点赞, 收藏
     */
    fun userAction(userActionReq: UserActionReq) {
        launch(
            block = {
                val result = RepositoryMgr.userAction(userActionReq)
                if (result.code == 200) {
                    GlobalMgr.collect.postValue(Triple(userActionReq.creation_id, userActionReq.action_type == DO_ACTION, System.currentTimeMillis()))
                    GlobalMgr.thumbUp.postValue(true)
                }
            }
        )
    }

    /**
     * 关注、取消关注
     */
    fun follow(followReq: FollowReq) {
        launch(
            block = {
                val result = RepositoryMgr.follow(followReq)
                GlobalMgr.follow.postValue(result.apiData())
            }
        )
    }

    /**
     * 删除作品
     */
    fun deleteProductById(createId: Int) {
        launch(
            block = {
                val result = RepositoryMgr.delProductById(createId)
                deleteResult.postValue(result.code == 200)
            }
        )
    }
}